//
//  LogonViewController.m
//  EntBoostChat
//
//  Created by zhong zf on 14-8-2.
//  Copyright (c) 2014年 entboost. All rights reserved.
//

#import "LogonViewController.h"
#import "AppDelegate.h"
#import "MainViewController.h"
#import "ENTBoost.h"
#import "ENTBoostChat.h"
#import "BlockUtility.h"
#import "CHKeychain.h"

@interface LogonViewController ()

@end

@implementation LogonViewController

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {

    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.serverAddressLabel.text = ENTBoostKit.serverAddress;
    self.accountTextField.text = [CHKeychain userName];
    self.passwordTextField.text = [CHKeychain password];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)loginButtonPressed:(id)sender
{
    //隐藏键盘
    [self.accountTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    
    //获取用户账号和密码
    NSString* account = [self.accountTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString* password = [self.passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    if(account.length==0 || password.length==0)
    {
        [[[UIAlertView alloc] initWithTitle:@"操作错误" message:@"用户名和密码不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
        return;
    }
    
    self.loginButton.enabled = NO;
    
    [CHKeychain saveUserName:account AndPassword:password]; //用户账号和密码保存至操作系统密码链
    
    __weak typeof(self) safeSelf = self;
    [BlockUtility performBlockInGlobalQueue:^{ //在并发线程中执行
        ENTBoostKit* ebKit = [ENTBoostKit sharedToolKit];
        [ebKit logonSyncWithAccount:account password:password onCompletion:^(EBAccountInfo *accountInfo) {
            NSLog(@"用户登录成功");
            [[NSNotificationCenter defaultCenter] postNotificationName:EBCHAT_NOTIFICATION_MANUAL_LOGON_SUCCESS object:self userInfo:nil];
            safeSelf.loginButton.enabled = YES;
        } onFailure:^(NSError *error) {
            NSLog(@"登录恩布通讯服务失败, code = %i, msg = %@", error.code, error.localizedDescription);
            [BlockUtility performBlockInMainQueue:^{
                NSString* msg = [NSString stringWithFormat:@"登录失败, code = %i, msg = %@", error.code, error.localizedDescription];
                [[[UIAlertView alloc] initWithTitle:@"" message:msg delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil] show];
            }];
            safeSelf.loginButton.enabled = YES;
        }];
    }];

}

//点击界面背景事件
- (IBAction)backgroundTap:(id)sender
{
    //隐藏键盘
    [self.accountTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
}

//用户账号输入完毕后事件
- (IBAction)accountDidEndOnExit:(id)sender
{
    [self.passwordTextField becomeFirstResponder];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
